﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.ETL
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class TablePartitionOutput:ResultBase 
    {
        private int _TotalParts;
        /// <summary></summary>
        public int TotalParts
        {
            get
            {
                return this._TotalParts;
            }
            set
            {
                this._TotalParts = value;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public TablePartitionOutput():base(Guid.Empty,Guid.Empty)
        {
            this._TotalParts = 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="componentID"></param>
        /// <param name="jobTicketID"></param>
        /// <param name="totalParts"></param>
        public TablePartitionOutput(Guid componentID, Guid jobTicketID, int totalParts) : base(componentID, jobTicketID)
        {
            this._TotalParts = totalParts;
        }

        #region Overrides of ResultBase

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xDoc"></param>
        /// <param name="parentNode"></param>
        /// <returns></returns>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = XmlDataUtil.AddElement(ref xDoc, parentNode, "Output");
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "Type", this.GetType().FullName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "WorkerID", this.OwnerWorkerInstanceID.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FinishTime", this.FinishTime.ToString());
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TotalParts",this.TotalParts.ToString());
            return xNode;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="xNode"></param>
        /// <returns></returns>
        public override ResultBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                TablePartitionOutput output = new TablePartitionOutput();
                // output.OwnerComponentID = new Guid(xNode.Attributes.GetNamedItem("ComponentID").Value);
                output.OwnerWorkerInstanceID = new Guid(xNode.Attributes.GetNamedItem("WorkerID").Value);
                output.FinishTime = DateTime.Parse(xNode.Attributes.GetNamedItem("FinishTime").Value);
                output.TotalParts = int.Parse(XmlDataUtil.GetAttributeValue(xNode, "TotalParts", "0"));
                return output;
            }
            else
            {
                throw new Exception("Unable to instantiate output: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
